//
//  EssenceViewController.m
//  gosh
//
//  Created by 许小军 on 16/9/22.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "EssenceViewController.h"
#import "XXJALLTableViewController.h"
#import "XXJVideoTableViewController.h"
#import "XXJVioceTableViewController.h"
#import "XXJPictureTableViewController.h"
#import "XXJWordTableViewController.h"
@interface EssenceViewController ()<UIScrollViewDelegate>
{
    UIView * _titleView;
    UIView * _lineView;
    UIButton * _previousBtn;
    UIScrollView * _scrollerView;
}
@end

@implementation EssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
    
    [self addChildVIew];
    
    [self setUpScrollerView];
    [self setUpNavBar];
    
    
    
    [self setUpTitleView];
    
    
    
}


-(void)setUpScrollerView
{
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    UIScrollView * scrollerView=[[UIScrollView alloc]init];
    _scrollerView=scrollerView;
    scrollerView.frame=self.view.bounds;
    scrollerView.showsHorizontalScrollIndicator=NO;
    scrollerView.showsVerticalScrollIndicator=NO;
    scrollerView.pagingEnabled=YES;
    scrollerView.delegate=self;
    scrollerView.scrollsToTop=NO;
    [self.view addSubview:scrollerView];
    
//    for (int i=0;i<self.childViewControllers.count; i++) {
//        UIView * childView=self.childViewControllers[i].view;
//        childView.frame=CGRectMake(i*self.view.cur_w, 0, self.view.cur_w, self.view.cur_h);
//        [scrollerView addSubview:childView];
//        
//    }
    
    scrollerView.contentSize=CGSizeMake(self.view.cur_w*5, 0);
    
    
}

-(void)addChildVIew
{
    [self addChildViewController:[[XXJALLTableViewController alloc]init]];
    [self addChildViewController:[[XXJVideoTableViewController alloc]init]];
    [self addChildViewController:[[XXJVioceTableViewController alloc]init]];
    [self addChildViewController:[[XXJPictureTableViewController alloc]init]];
    [self addChildViewController:[[XXJWordTableViewController alloc]init]];
    
}

-(void)setUpTitleView
{
    UIView * titleView=[[UIView alloc]init];
    _titleView=titleView;
    titleView.backgroundColor=[[UIColor whiteColor]colorWithAlphaComponent:0.5];
    titleView.frame=CGRectMake(0, 64, self.view.cur_w, 35);
    [self.view addSubview:titleView];
    
    [self setUpTitleBtn];
    
    [self setUpLine];
    
    UIButton * firstBtn=_titleView.subviews[0];
    _lineView.cur_w=[firstBtn.currentTitle sizeWithAttributes:@{NSFontAttributeName:firstBtn.titleLabel.font}].width+10;
    _lineView.cur_center_x=firstBtn.cur_center_x;
    //[firstBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    //firstBtn.selected=YES;
    [self btnClick:firstBtn];
    
    
    
}

-(void)setUpTitleBtn
{
    NSArray * array=@[@"全部",@"视频",@"声音",@"图片",@"段子"];
    
    CGFloat titleBtnW=self.view.cur_w/5.0;
    CGFloat titleBtnH=_titleView.cur_h;
    for (int i=0; i<5; i++)
    {
        UIButton * btn=[[UIButton alloc]init];
        btn.titleLabel.font=[UIFont systemFontOfSize:16];
        btn.frame=CGRectMake(titleBtnW*i, 0, titleBtnW, titleBtnH);
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        //[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [_titleView addSubview:btn];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}

-(void)setUpLine
{
    UIView * lineView=[[UIView alloc]init];
    _lineView=lineView;
    lineView.backgroundColor=[UIColor redColor];
    lineView.cur_h=2;
    lineView.cur_y=_titleView.cur_h-lineView.cur_h;
    [_titleView addSubview:lineView];
}

-(void)btnClick:(UIButton *)button
{
    if (_previousBtn==button) {
       
        [[NSNotificationCenter defaultCenter]postNotificationName:TitleBtnDoubleClicked object:nil];
    }
    
    [self dealBtnClick:button];
}

-(void)dealBtnClick:(UIButton *)button
{
    //_previousBtn.selected=NO;
    [_previousBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    //button.selected=YES;
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    //动画效果
    [UIView animateWithDuration:0.25 animations:^{
        _lineView.cur_w=[button.currentTitle sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}].width+10;
        _lineView.cur_center_x=button.cur_center_x;
        
        NSUInteger index=[_titleView.subviews indexOfObject:button];
        _scrollerView.contentOffset=CGPointMake(_scrollerView.cur_w*index, 0);
    }completion:^(BOOL finished) {
        //哪个按钮对应创建哪个表视图
        NSUInteger index=[_titleView.subviews indexOfObject:button];
        UIView * view=self.childViewControllers[index].view;
        if (view.superview)
        {
            return ;
        }
        view.frame=CGRectMake(index*_scrollerView.cur_w, 0, _scrollerView.cur_w, _scrollerView.cur_h);
        [_scrollerView addSubview:view];
    }];
    _previousBtn=button;
    
    
    //scrollsToTop  只能有一个视图的scrollsToTop为YES,才能有效
    //设置index位置对应的tableview.scrollsToTop=yes,其他设置为no
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController * childVc=self.childViewControllers[i];
        if (!childVc.isViewLoaded) {
            continue;//如果控制器的view没有记载，就继续for循环
        }
        UIScrollView * scroller=(UIScrollView *)childVc.view;
        if (![scroller isKindOfClass:[UIScrollView class]]) {
            continue;//如果不是UIScrollView，继续循环
        }
        if (i==[_titleView.subviews indexOfObject:button]) {
            scroller.scrollsToTop=YES;
        }
        else
        {
            scroller.scrollsToTop=NO;
        }
        
        
    }
    
    
    
    
    
}

-(void)setUpNavBar
{
    UIButton * leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"nav_item_game_icon"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"nav_item_game_click_icon"] forState:UIControlStateHighlighted];
    [leftBtn sizeToFit];
    [leftBtn addTarget:self action:@selector(leftBtnn) forControlEvents:UIControlEventTouchUpInside];
    //UIView * view=[[UIView alloc]initWithFrame:leftBtn.bounds];
    //[view addSubview:leftBtn];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    
    
    
    UIButton * rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"navigationButtonRandom"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"navigationButtonRandomClick"] forState:UIControlStateHighlighted];
    [rightBtn sizeToFit];
    [rightBtn addTarget:self action:@selector(rightBtnn) forControlEvents:UIControlEventTouchUpInside];
    UIView * view1=[[UIView alloc]initWithFrame:rightBtn.bounds];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:view1];
    [view1 addSubview:rightBtn];
    self.navigationItem.titleView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    
}


#pragma mark --UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index=_scrollerView.contentOffset.x/_scrollerView.cur_w;
    UIButton * btn=_titleView.subviews[index];
    [self btnClick:btn];
}

-(void)leftBtnn
{
    
}

-(void)rightBtnn
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
